Walkthrough 13-1: Process items in a collection using the For Each scope
In this walkthrough, you split a collection and process each item in it. You will:
· Use the For Each scope element to process each item in a collection individually.
· Change the value of an item inside the scope.
· Examine the payload before, during, and after the scope.
· Look at the thread used to process each item.
Starting file
If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.
Stop the syncDBaccountsWithPostal flow so it does not run
1. Return to accounts.xml.
2. In the properties view for the syncDBaccountsWithPostal flow, set the initial state to stopped.
3. Right-click in the canvas and select Collapse All.
Add a For Each scope
4. Expand getCSVaccounts.
5. In the Mule Palette, select Core.
6. Locate the For Each scope and drag and drop it before the Logger.
Transform the input to a collection
7. Add a Transform Message component before the For Each scope.
8. Set its display name to CSV to Java.
9. In the Transform Message properties view, leave the output type set to java and set the expression to payload.
Process each element in the For Each scope
10. Add a Set Payload transformer and a Logger to the For Each scope.
11. In the Set Payload properties view, set the display name and value to processed.
12. Set the Logger display name to payload and have it display the payload.
Change the File listener so it does not rename files
13. In the accounts.csv properties view, delete the Rename to value.
Debug the application
14. Add a breakpoint to the Transform Message component.
15. Save the file, debug the project, and do not clear application data.
16. Return to the student files in your computer’s file browser.
17. Rename accounts.csv.backup to accounts.csv in the input folder.
18. Return to Anypoint Studio; application execution should have stopped at the Transform Message component.
19. In the Mule Debugger view, look at the payload type and value.
20. Step to the For Each scope; the payload should now be an ArrayList of LinkedHashMaps.
21. Step into the Set Payload in the For Each scope; the payload should now be a LinkedHashMap.
22. Expand Variables; you should see a counter variable.
23. Step again; you should see the payload for this record inside the scope has been set to the string, processed.
24. Step again and look at the payload and counter for the second record.
25. Step through the application to the Logger after the For Each scope; the payload should be equal to the original ArrayList of HashMaps and not a list of processed strings.
26. Step to the end of the application.
27. Stop the project and switch perspectives.
Look at the processing threads
28. In the console, locate the thread number used to process each item in the collection; the same thread should be used for each: cpuIntensive.01 in the following screenshot.